package com.admission.web.admin;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.ArrayInfo;
import com.admission.model.TeacherView;
import com.admission.service.EnvironmentManager;
import com.admission.service.TeacherViewManager;
import com.admission.util.NewFileName;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;
@Namespace("/admin")
public class TeacherViewAdminAction extends CrudActionSupport<TeacherView>{

	private static final long serialVersionUID = 488185923443490311L;
	private String id;
	private TeacherView entity;
	private File picture;
	private String pictureFileName;
	private Page<TeacherView> page = new Page<TeacherView>(PageUtils.DEFAULT_PAGE_SIZE);
    private TeacherViewManager teacherViewManager;
    private String ids;
    private String status; //控制名师介绍开关功能
    private String value;
    private List<ArrayInfo> arrayList;
    private EnvironmentManager environmentManager;
    
	public TeacherView getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		teacherViewManager.getByordTime(page);
		return SUCCESS;
	}
	/**
	 * 通过id获取
	 * @param id
	 * @return TeacherView
	 */
	@Override
	public String input() throws Exception {
		entity=teacherViewManager.getTeacherViewById(id); 
		arrayList=environmentManager.getByBigType(value);
		return "detail";
	}
	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		// ----------------上传图片方法------------------
				String serverrealPath = ServletActionContext.getServletContext()
							.getRealPath("/upload");
				File dir = new File(serverrealPath);
				if (!dir.exists()) {
					dir.mkdir();
				}
				String subPath = null;
					if (picture != null) {
						subPath = new String();
						subPath = NewFileName.genNewFileName(pictureFileName,0);
						FileUtils.copyFile(picture, new File(serverrealPath,subPath));
					}
				    try {
					   teacherViewManager.save(entity,subPath);
				   } catch (Exception e) {
					   this.addActionMessage("操作失败，请重新操作");
				   }  
				  return list();
				}

	   public String add() throws Exception{
		   arrayList=environmentManager.getByBigType(value);
		   return "add";
	   }

	@Override
	public String delete() throws Exception {
		try{
		   teacherViewManager.delete(id);
	   } catch (Exception e) {
		   this.addActionMessage("操作失败，请重新操作");
	   }  
		return list();
	}
	/**
	 * 批量删除
	 */
	public String deleteMore() throws Exception {
		try{
		   teacherViewManager.deleteMore(ids);
	   } catch (Exception e) {
		   this.addActionMessage("操作失败，请重新操作");
	   }  
		return list();
	}
	@Override
	protected void prepareModel() throws Exception {
		if(StringUtils.isEmpty(id)){
			entity = new TeacherView();
		}else{
			entity = teacherViewManager.getTeacherViewById(id);
		}
		
	}
	//改变名师介绍开关功能
	public String changeConsult() throws Exception{
		teacherViewManager.changeConsult(status);
		if(status.equals("1"))
			Struts2Utils.renderText("true");
		else
			Struts2Utils.renderText("false");
		return null;
}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public TeacherView getEntity() {
		return entity;
	}

	public void setEntity(TeacherView entity) {
		this.entity = entity;
	}

	public Page<TeacherView> getPage() {
		return page;
	}

	public void setPage(Page<TeacherView> page) {
		this.page = page;
	}

	public TeacherViewManager getTeacherViewManager() {
		return teacherViewManager;
	}
    @Autowired
	public void setTeacherViewManager(TeacherViewManager teacherViewManager) {
		this.teacherViewManager = teacherViewManager;
	}

	public File getPicture() {
		return picture;
	}

	public void setPicture(File picture) {
		this.picture = picture;
	}

	public String getPictureFileName() {
		return pictureFileName;
	}

	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public List<ArrayInfo> getArrayList() {
		return arrayList;
	}

	public void setArrayList(List<ArrayInfo> arrayList) {
		this.arrayList = arrayList;
	}

	public EnvironmentManager getEnvironmentManager() {
		return environmentManager;
	}
	@Autowired
	public void setEnvironmentManager(EnvironmentManager environmentManager) {
		this.environmentManager = environmentManager;
	}
    
   

}
